<?php

namespace app\home\controller;

use app\common\controller\Home;

class Teacher extends Home
{
   public $NoLogin = ['index','info'];

   public function __construct()
   {
        parent::__construct();

        $this->SubjectModel = model('Subject.Subject');
        $this->TeacherModel = model('Teacher.Teacher');
        $this->CategoryModel = model('Subject.Category');
        $this->FollowModel = model('Teacher.Follow');
   }

       //名师查找
       public function index()
       {   
           if($this->request->isAjax())    
           {
               $page = $this->request->param('page',1,'trim');
               $limit = $this->request->param('limit',10,'trim');
               $keywords = $this->request->param('keywords','','trim');
   
               //偏移量
               $start = ($page-1)*$limit;
   
               $where = [];
   
               if(!empty($keywords))
               {
                   $where['name|job'] = ['LIKE',"%$keywords%"];
               }
   
               //查询数据总条数
               $count = $this ->TeacherModel->where($where)->count();
   
               //查询数据
               $list = $this->TeacherModel
                            ->where($where)
                            ->limit($start,$limit)
                            ->select();
   
               $data = [
                   'count' => $count,
                   'list'  => $list
               ];
   
               if($list)
               {
                   //成功返回
                   $this->success('返回列表',null,$data);
                   exit;
               }else
               {
                   //失败返回
                   $this->error('暂无数据',null,$data);
                   exit;
               }
           }
           return $this->view->fetch();
       }

       //名师详情界面
       public function info()
       {    
            $teacherid = $this->request->param('teacherid',0,'trim');

            //根据教师id查找教师
            $teacher = $this->TeacherModel->find($teacherid);
            
            if(!$teacher)
            {
                $this->error('教师不存在');
                exit;
            }

            //获取用户是否登录的信息
            $login = $this->IsLogin(false);

            if($login)
            {
                //查询是否有关注过当前教师
                $where = [
                    'teacherid' => $teacherid,
                    'busid' =>$login['id']
                ];

               $check =  $this->FollowModel->where($where)->find();

               //增加自定义属性
               $teacher['follow'] = $check ? true : false;
            }else 
            {
                $teacher['follow'] = false;
            }

            //判断是否有ajax请求
            if($this->request->isAjax())
        {
            $page = $this->request->param('page',1,'trim');
            $limit = $this->request->param('limit',10,'trim');
            $keywords = $this->request->param('keywords','','trim');

            //偏移量
            $start = ($page-1)*$limit;

            $where = ['teacherid' => $teacherid];

            if(!empty($keywords))
            {
                $where['title'] = ['LIKE',"%$keywords%"];
            }

            //查询数据总条数
            $count = $this ->SubjectModel->where($where)->count();

            //查询数据
            $list = $this->SubjectModel
                         ->with(['category'])
                         ->where($where)
                         ->limit($start,$limit)
                         ->select();

            $data = [
                'count' => $count,
                'list'  => $list
            ];

            if($list)
            {
                //成功返回
                $this->success('返回列表',null,$data);
                exit;
            }else
            {
                //失败返回
                $this->error('暂无数据',null,$data);
                exit;
            }
            }


            //传递数据到前台模板
            $this->assign([
                'teacher' => $teacher
            ]);

            return $this->view->fetch();
       }

       //关注和取关操作
       public function follow()
       {
            $teacherid = $this->request->param('teacherid',0,'trim');

            //查询老师信息
            $teacher = $this->TeacherModel->find($teacherid);

            if(!$teacher)
            {
                $this->error('名师信息不存在'); 
                exit;
            }

            //判断当前用户是否已经关注了当前点击教师
            $where = [
                'teacherid' => $teacherid,
                'busid' => $this ->view->AutoLogin['id']
            ];

            $check = $this->FollowModel->where($where)->find();
            $msg = $result ='';//初始化

            //若能找出记录，说明已经关注过当前教师
            if($check)
            {   
                $this->FollowModel->destroy($check['id']);
                $msg = '取消关注成功';
                
            }else
            {
                //若check为空，则说明用户还未关注当前教师
                //组装数据
                $data = [
                    'teacherid' => $teacherid,
                    'busid' => $this->view->AutoLogin['id']
                ];
                $result =  $this->FollowModel->validate('common/Teacher/Follow')->save($data);
                $msg = '关注成功';

            }

            if($result === FALSE)
            {
                $this->error($this->FollowModel->getError());
                exit;
            }else
            {
                $this->success($msg);
                exit;
            }
       }
}
